#include "main.h"

//
// The core application. The structure of GLUT forces this to be a global variable.
//
static Application app;

void keyboardDownCallback(unsigned char key, int x, int y);
void keyboardUpCallback(unsigned char key, int x, int y);
void displayCallback();
void setup();

int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
    glutInitWindowPosition(20, 20);

    app.InitParameters();

    if(app.Parameters().testingModel)
    {
        glutInitWindowSize(Constants::windowSizeX, Constants::windowSizeY / 2);
    }
    else
    {
        glutInitWindowSize(Constants::windowSizeX, Constants::windowSizeY);
    }

    glutCreateWindow("GGL");

    setup();

    glutKeyboardFunc(&keyboardDownCallback);
    glutKeyboardUpFunc(&keyboardUpCallback);
    glutDisplayFunc(&displayCallback);

    glutMainLoop();

    return 0;
}

void timerTickCallback(int value)
{
    //
    // Update the sculpture animation
    //
    app.Step();

    glutPostRedisplay();

    //
    // schedule new timer event
    //
    glutTimerFunc( (int)(1000.0f / app.Parameters().framesPerSecond), timerTickCallback, 0);
}

void keyboardDownCallback(unsigned char key, int x, int y)
{
    app.KeyDown(key);
    /*switch (key)
    {
    case '\x1B':
        exit(EXIT_SUCCESS);
        break;
    }*/
}

void keyboardUpCallback(unsigned char key, int x, int y)
{
    app.KeyUp(key);
}


void setup()
{
    app.Init();

    glutTimerFunc( (int)(1000.0f / app.Parameters().framesPerSecond), timerTickCallback, 0);
}


void displayCallback()
{
    glClear(GL_COLOR_BUFFER_BIT);

    app.Draw();

    glFlush();

    glutSwapBuffers();
}
